﻿using Company.Communication.Serial;
using Company.Hardware.Acs;
using Company.Share.Prism;
using Company.UI.Language;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

namespace Company.Main.ViewModels
{
    public class MainViewModel : BindableBase
    {
        public IRegionManager RegionManager { get; }
        public AcsController AcsController { get; }
        public ISerialCommunication SerialCommunication { get; }
        public Window MainWindow { get; set; }
        public DelegateCommand WindowClosedCommand { get;}
        public DelegateCommand WindowRestoredCommand { get;}
        public DelegateCommand WindowMinimizedCommand { get;}
        public DelegateCommand<object> ViewNavigationCommand { get; }
        public MainViewModel(IRegionManager regionManager, 
            IContainerProvider containerProvider,
            IAcsController acsController,
            ISerialCommunication serialCommunication)
        {
            // 从容器获取MainWindow实例
            if (containerProvider.IsRegistered<Window>(ViewNames.MainWindow))
            {
                MainWindow = containerProvider.Resolve<Window>(ViewNames.MainWindow);
            }
            WindowClosedCommand = new DelegateCommand(WindowClosed);
            WindowRestoredCommand = new DelegateCommand(WindowRestored);
            WindowMinimizedCommand = new DelegateCommand(WindowMinimized);
            RegionManager = regionManager;
            ViewNavigationCommand = new DelegateCommand<object>(ViewNavigation);
            AcsController = acsController as AcsController;
            SerialCommunication = serialCommunication;
        }
        /// <summary>
        /// 窗体最小化命令
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void WindowMinimized()
        {
            if (MainWindow == null) return;
            MainWindow.WindowState = WindowState.Minimized;
        }

        /// <summary>
        /// 窗体最大化命令
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void WindowRestored()
        {
            if (MainWindow == null) return;
            if (MainWindow.WindowState == WindowState.Normal)
                MainWindow.WindowState = WindowState.Maximized;
            else
                MainWindow.WindowState = WindowState.Normal;
        }

        /// <summary>
        /// 窗体关闭命令
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void WindowClosed()
        {
            //AcsController.UnregisterEmergencyStop();
            SerialCommunication.ClosePort();
            AcsController.Disconnect();
            Environment.Exit(0);
        }
        /// <summary>
        /// 视图导航
        /// </summary>
        /// <param name="obj"></param>
        private void ViewNavigation(object obj)
        {
            var viewName = obj.ToString();
            if (viewName == null) return;
            switch (viewName)
            {
                case ViewNames.HomeView:
                    RegionManager.RequestNavigate(RegionNames.NavigationRegion, ViewNames.HomeView);
                    break;
                case ViewNames.MotionView:
                    RegionManager.RequestNavigate(RegionNames.NavigationRegion, ViewNames.MotionView);
                    break;
                case ViewNames.IOView:
                    RegionManager.RequestNavigate(RegionNames.NavigationRegion, ViewNames.IOView);
                    break;
                case ViewNames.BufferManageView:
                    RegionManager.RequestNavigate(RegionNames.NavigationRegion, ViewNames.BufferManageView);
                    break;
                case ViewNames.TerminalCommandView:
                    RegionManager.RequestNavigate(RegionNames.NavigationRegion, ViewNames.TerminalCommandView);
                    break;
                case ViewNames.SystemSettingView:
                    RegionManager.RequestNavigate(RegionNames.NavigationRegion, ViewNames.SystemSettingView);
                    break;
                default:
                    RegionManager.RequestNavigate(RegionNames.NavigationRegion, ViewNames.HomeView);
                    break;
            }
        }
    }
}
